﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataLib.Accounts.Bus;
using DataLib.BLL;
using DataLib.Common;

namespace Ewts.Production
{
    public partial class ProduceMaterial : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["UserInfo"] != null)
                {
                    User curUser = new DataLib.Accounts.Bus.User();
                    curUser = (User)Session["UserInfo"];
                    string outid = Request["outid"];
                    string lid = Request["insid"];
                    if (outid != null && outid != "0")
                    {
                        BarCodeDataBind(int.Parse(outid));
                    }
                    else if (lid != null && lid != "0")
                    {
                        BarCodeDataBind(bindInsid(int.Parse(lid)));

                    }
                    else
                    {
                        DataLib.Common.MessageBox.ShowAndRedirect(Page, "URL非法!！", Request.UrlReferrer.ToString());
                    }
                 
                }
                else
                {
                    Response.Redirect("/Accounts/Login.aspx");
                    return;
                }

            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string insid = hidInStoID.Value;
            string outid = hidOutid.Value;
            string month = hidmonth.Value;

            if (string.IsNullOrEmpty(insid) || insid == "0")
            {
               DataLib.Common.MessageBox.Show(this,"请插入数据,再提交!");
                return;
            }
            DataLib.BLL.Productions bllpro = new Productions();
            int flag = bllpro.LingliaoSubmit(Convert.ToInt32(insid), Convert.ToInt32(outid), month);
            if ( flag== 0)
            {
               // OutStorageAndInStorageSetStatus(Convert.ToInt32(outid));
                //9+878756
                //DataLib.Common.MessageBox.ShowAndRedirect(Page, "领料完成！", "/Production/ProduceWaitInStorageList.aspx");
                DataLib.Common.MessageBox.ShowAndRedirect(Page, "领料完成！", "/RDLC/ProductionRDLC.aspx?outStorID=" + outid);
            }
            else if(flag==1)
            {
                DataLib.Common.MessageBox.ShowAndRedirect(Page, "领料已经提交，可继续领料或者进行生产返还录入!！", "/Production/ProduceWaitInStorageList.aspx");
            }
            else 
            {
                DataLib.Common.MessageBox.ShowAndRedirect(Page, "领料单总重量为0，请录入物料再提交!！", Request.Url.ToString());
            }

            

        }
        protected string GetIsLock(string islock)
        {
            if (Boolean.Parse(islock))
                return "checked";
            else
                return "";
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="outid"></param>
        protected void BarCodeDataBind(int ? outid)
        {
            hidOutid.Value = outid.ToString();

            //为隐藏控件赋值
            DataLib.Model.OutStorage outmodel = new DataLib.BLL.OutStorage().GetModel(outid);
            if (outmodel != null && outmodel.id > 0)
            {
                lblTotalweight.InnerHtml = outmodel.totalWeight.ToString();
                hidLoadnum.Value = outmodel.VLoadNum;
                hidoutsno.Value = outmodel.outStorageNo;
                hidInDepart.Value = outmodel.inDepart;
                hidOutDepart.Value = outmodel.OutDepart;
                hidmonth.Value = outmodel.clearmonth;
                hiddenOper.Value = outmodel.Operator;
                lblOutNo.InnerHtml = outmodel.outStorageNo;
               
            }

            DataLib.BLL.Productions bll = new Productions();
            DataTable dt=new DataTable();
            dt = bll.GetBarcodeList((int)outid,"RB").Tables[0];
            rept.DataSource = dt;
            rept.DataBind();
            if (dt != null &&dt.Rows.Count>0)
            {
                hidInStoID.Value = dt.Rows[0]["InStrorageID"].ToString();
                bindInsid(int.Parse(hidInStoID.Value));
            }
        }

        protected int bindInsid(int insid)
        {
            hidInStoID.Value = insid.ToString();
            DataLib.BLL.InStorage bll = new InStorage();
            DataLib.BLL.Productions pbll = new Productions();
            DataLib.Model.InStorage model = bll.GetModel(insid);
            lblInsno.InnerHtml = model.wrlno;
            lblInsTotal.InnerHtml = pbll.GetInsTotal(insid.ToString()).ToString();


             return (int)model.outid;
        }

    }
}